@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
REM
REM QVGA video encoding
REM
REM for /f "usebackq tokens=1-3" %i in (`tasklist ^| findstr /i "neroaacenc"`) do setpriority -lowest %j

if "%~1" equ "" call :usage && exit /b 99

set x264_exe=d:\zerox\bin\x264.exe
set lame_exe=d:\zerox\bin\lame.exe
set neroaacenc_exe=d:\zerox\bin\NeroDigital\neroAacEnc.exe
set azid_exe=d:\zerox\bin\azid\azid.exe
set faad_exe=d:\zerox\bin\faad.exe
set faac_exe=d:\zerox\bin\faac.exe

REM ============================================

:loop_args
	if "%~1" equ "-vi"  (if not "%~2" equ "" (set video_input=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-vo"  (if not "%~2" equ "" (set video_output=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-ai"  (if not "%~2" equ "" (set audio_input=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-ao"  (if not "%~2" equ "" (set audio_output=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-ad"  (if not "%~2" equ "" (set audio_decoding=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-o"   (if not "%~2" equ "" (set mp4_output=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-br"  (if not "%~2" equ "" (set bitrate=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-fps" (if not "%~2" equ "" (set fps=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-r"   (if not "%~2" equ "" (set resolution=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-i"   (if not "%~2" equ "" (set max_i_interval=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-ii"  (if not "%~2" equ "" (set min_i_interval=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-b"   (if not "%~2" equ "" (set bframes=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-s"   (if not "%~2" equ "" (set stats=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-p"   (if not "%~2" equ "" (set pass=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-pp"  (if not "%~2" equ "" (set profile=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-ab"  (if not "%~2" equ "" (set aac_bitrate=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-aq"  (if not "%~2" equ "" (set aac_quality=%~2&& shift) else (call :error_usage && goto :eof))
	if "%~1" equ "-mp3" set audio_encoding=mp3
	if "%~1" equ "-aac" set audio_encoding=aac
	if "%~1" equ "-faac" set audio_encoding=faac

	shift
	if not "%~1" equ "" goto loop_args

REM ============================================

if defined video_input if not defined video_output for %%i in (!video_input!) do set video_output=%%~ni.264
if not defined audio_encoding set audio_encoding=aac
if defined audio_input if not defined audio_output for %%i in (!audio_input!) do set audio_output=%%~ni.!audio_encoding!

echo [!DATE! !TIME!] Input/Output
for %%i in (video_input video_output audio_input audio_output mp4_output) do echo %%i: [!%%i!]

REM Profiles definition for video encoding
set fast_profile=--bframes 8 --b-pyramid --weightb --direct auto --filter -2,-1 --analyse p8x8,b8x8,i4x4,i8x8 --8x8dct --merange 8 --threads auto --thread-input --no-psnr --no-ssim
set slow_profile=--ref 3 --bframes 16 --b-pyramid --weightb --direct auto --filter -2,-1 --subme 6 --trellis 1 --analyse p8x8,b8x8,i4x4,i8x8 --8x8dct --me umh --merange 12 --threads auto --thread-input --no-psnr --no-ssim
set slower_profile=--ref 5 --mixed-refs --bframes 16 --b-pyramid --b-rdo --bime --weightb --direct auto --filter -2,-1 --subme 6 --trellis 1 --analyse p8x8,b8x8,i4x4,i8x8 --8x8dct --me umh --threads auto --thread-input --no-psnr --no-ssim
set slowest_profile=--ref 10 --mixed-refs --no-fast-pskip --bframes 16 --b-pyramid --b-rdo --bime --weightb --direct auto --filter -2,-1 --subme 6 --trellis 2 --analyse p8x8,b8x8,i4x4,i8x8 --8x8dct --me umh --threads auto --thread-input --no-dct-decimate --no-psnr --no-ssim
set insane_profile=--ref 16 --mixed-refs --no-fast-pskip --bframes 16 --b-pyramid --b-rdo --bime --weightb --direct auto --filter -2,-1 --subme 7 --trellis 2 --analyse p8x8,b8x8,i4x4,i8x8 --8x8dct --me umh --threads auto --thread-input --no-dct-decimate --no-psnr --no-ssim
set anime_profile=--ref 8 --mixed-refs --bframes 16 --b-pyramid --b-rdo --bime --weightb --direct auto --filter 1,1 --subme 6 --trellis 1 --analyse p8x8,b8x8,i4x4,i8x8 --8x8dct --me umh --merange 12 --threads auto --thread-input --no-psnr --no-ssim

REM video encoding
if defined video_input (
	REM options
	if not defined fps (echo ERROR: fps not set) && goto :eof
	if not defined stats for %%i in (!video_input!) do set stats=%%~ni.stats.log
	if not defined profile set profile=slower
	if not defined bframes set bframes=4
	if not defined bitrate set bitrate=192
	if not defined min_i_interval set /a min_i_interval=!fps:~0,2!+1
	set fourth_char=!fps:~3,1!
	if "!fourth_char!" equ "" (set /a min_i_interval=min_i_interval-1) else if not !fourth_char! gtr 0 set /a min_i_interval=min_i_interval-1
	if !min_i_interval! lss 10 (
		echo ERROR: 'max_i_interval': invalid! ^(not set or cannot deduce from fps: !fps!^) && goto :eof
	) else (if not defined max_i_interval set /a max_i_interval=min_i_interval*5)
	for %%p in (!profile!) do set profile_use=!%%~p_profile!
	REM run
	echo [!DATE! !TIME!] Multi-pass configuration
	for %%i in (profile bitrate fps max_i_interval min_i_interval bframes resolution pass) do echo %%i: [!%%i!]
	if not "!pass!" equ "2" (
		echo [!DATE! !TIME!] Multi-pass: 1st pass

		!x264_exe! !profile_use! ^
					--progress   --stats !stats!  --output "!video_output!"^
					--fps !fps!  --bitrate !bitrate! ^
					--keyint !max_i_interval!     --min-keyint !min_i_interval!  --bframes !bframes! ^
					--pass 1 "!video_input!" !resolution!
	)
	if not "!pass!" equ "1" (
		echo [!DATE! !TIME!] Multi-pass: 2nd pass begin
		!x264_exe! !profile_use! ^
					--progress   --stats !stats!  --output "!video_output!"^
					--fps !fps!  --bitrate !bitrate! ^
					--keyint !max_i_interval!     --min-keyint !min_i_interval!  --bframes !bframes! ^
					--pass 2 "!video_input!" !resolution!
	)
)

if defined audio_input (
	if defined audio_decoding (
		echo [!DATE! !TIME!] Audio decoding from !audio_decoding!
		for %%i in (!audio_output!) do set audio_wav=%%~ni.wav
		if !audio_decoding! equ ac3 !azid_exe! --preset=2ch --maximize --norm=on "!audio_input!" "!audio_output!"
		if !audio_decoding! equ mp3 !lame_exe! --decode "!audio_input!" "!audio_wav!"
		if !audio_decoding! equ aac !faad_exe! -d -o "!audio_wav!" "!audio_input!"
		if not exist !audio_wav! echo ERROR: unknown audio decoding: !audio_decoding!&&goto :eof
		set audio_input=!audio_wav!
	)
	echo [!DATE! !TIME!] Audio encoding from !audio_input! to !audio_output!
	if !audio_encoding! equ mp3 (
		!lame_exe! --vbr-new -V 9 -B 128 -o -m j --priority 0 "!audio_input!" "!audio_output!"
	)
	if !audio_encoding! equ aac (
		if not defined aac_bitrate set aac_bitrate=64
		if "!aac_bitrate:~3,1!" equ "" set /a aac_bitrate=aac_bitrate*1024
		!neroaacenc_exe! -2pass -he -br !aac_bitrate! -if "!audio_input!" -of "!audio_output!"
	)
	if !audio_encoding! equ faac (
		if defined aac_bitrate set faac_option=-b !aac_bitrate!
		if defined aac_quality set faac_option=-q !aac_quality!
		if not defined faac_option set faac_option=-q 120
		!faac_exe! -o "!audio_output!" !faac_option! "!audio_input!"
	)
)

if defined mp4_output if defined audio_output if defined video_output (
	echo [!DATE! !TIME!] MP4 muxing: !mp4_output!
	mp4box -new !mp4_output! -add !video_output! -add !audio_output! -fps !fps!
)
if defined mp4_output if not exist !mp4_output! echo ERROR: cannot mux MP4 output&&goto :eof
goto :end

:error_usage
	echo ********  Some error occured  ********
:usage
	echo ^`%~nx0^' script written by Xia Zuoling (v2, 2008-5-26 0:55:28)
	echo   All switchs, options are case-sensitive
	echo   Video:
	echo         -vi VIDEO_INPUT -vo VIDEO_OUTPUT  (accepts .AVS input)
	echo         -pp PROFILE (fast, slow, slower, slowest, insane, anime)
	echo         -br Kbps  -fps FLOAT  -r RESOLUTION (320x240)
	echo         -i MAX_IFrame_INT     -ii MIN_IFrame_INT    -b BFrames
	echo         -vmr VBV_MAX_BITRATE
	echo         -s STATS_LOG    -p PASS (1, 2, or null to enable both)
	echo   Audio:
	echo         -ai AUDIO_INPUT -ao AUDIO_OUTPUT  (accepts .MP3 or .WAV input)
	echo         -mp3  Use lame encoding       -aac  Use NeroDigital AAC encoding
	echo         -ad ac3^|aac^|mp3  Decode the input audio before encoding
	echo   General:
	echo         -o MP4_OUTPUT (invalid unless both video and audio are specified)
	goto :eof

:end
echo [!DATE! !TIME!] END
